package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.apache.catalina.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询学生
     *
     * @param
     * @return
     */
    @Override
    public PageResult<Student> listStudentPage(StudentQueryParam param) {
        // 分页对象
        IPage<Student> ipage = new Page<>(param.getPage(), param.getPageSize());
        Wrapper<Student> wrapper = Wrappers.<Student>lambdaQuery()
                .like(!StringUtils.isEmpty(param.getName()), Student::getName, param.getName())
                .eq(param.getDegree() != null, Student::getDegree, param.getDegree())
                .eq(param.getClazzId() != null, Student::getClazzId, param.getClazzId())
                .orderByDesc(Student::getUpdateTime);

        IPage<Student> ipageResult = page(ipage, wrapper);

        List<Student> studentList = ipageResult.getRecords();
        // 获取班级id集合
        List<Integer> clazzIdList = studentList.stream().map(student -> student.getClazzId()).distinct().toList();

        List<Clazz> clazzList = clazzService.listByIds(clazzIdList);

        studentList.forEach(student -> {
            clazzList.stream()
                    .filter(clazz -> clazz.getId().equals(student.getClazzId()))
                    .findFirst()
                    .ifPresent(clazz -> student.setClazzName(clazz.getName()));
        });
        return new PageResult<>(ipageResult.getTotal(), studentList);
    }

    /**
     * 新增学生信息
     *
     * @param student
     */
    @Override
    public void saveStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        return this.getById(id);
    }

    /**
     * 修改学生信息
     *
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    /**
     * 批量删除学生
     *
     * @param ids
     */
    @Override
    public void removeStudentById(List<Integer> ids) {
        removeByIds(ids);
    }

    /**
     * 根据id扣分
     *
     * @param id
     * @param score
     */
    @Override
    public void updateStudentScore(Integer id, Integer score) {

    }
}
